package com.liu.demo1.testDemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author liusiyu
 * @ClassName test
 * @date 2021年12月16日 14:47
 */
public class test {

    public static void main(String[] args) {
        boolean flag = true;
        int n = 1;
        while (flag){
            try {
                System.out.println("我开始第"+n+"次调用啦");
                test(n);
                System.out.println("我开始第"+n+"次睡觉啦");
                Thread.sleep(120000);
                n++;
            } catch (InterruptedException e) {
                e.printStackTrace();
                flag = false;
            }
        }
    }

    public static void test(int n){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        }catch (ClassNotFoundException e){
            System.out.println("找不到驱动程序，加载驱动失败");
            e.printStackTrace();
        }

        String url = "jdbc:mysql://10.10.0.141:3306/poclis002";
        String username = "lis_rw";
        String password = "a9Ip1tieLSg#";
        String sql = "select "+n+" from ldcode where code = '1' limit 1";
        Connection con = getCon(url,username,password);
        PreparedStatement stat = getStat(con,sql);
        ResultSet rs = executeQuery(stat);
        closeRs(rs);
        closeStmt(stat);
        closeConn(con);
    }

    public static Connection getCon(String url,String username,String password){
        Connection con = null;
        try {
            con = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }


    public static void closeConn(Connection conn){
        try {
            if(conn != null ){
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static PreparedStatement getStat(Connection conn,String sql){
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pstmt;

    }

    public static void closeStmt(Statement stmt){
        try {
            if(stmt != null ){
                stmt.close();
                stmt = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static ResultSet executeQuery(PreparedStatement stmt){
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static void closeRs(ResultSet rs){
        try {
            if(rs != null ){
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
